package 多线程.生产者消费者模型;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import static java.lang.Thread.sleep;

class Main {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(24);
        //生产者
        Thread t1 = new Thread(() -> {
            int count = 1;
            while(true){
                try {
                    queue.put(count);
                    System.out.println("value: " + count++);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });
        //消费者
        Thread t2 = new Thread(() -> {
        while(true){
            try {
                Integer out = queue.take();
                System.out.println("out:"+out);
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        });
        t1.start();
        t2.start();
    }
}

 class Main1 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(1024);
        queue.put("aaa");
        String elem = queue.take();
        System.out.println("elem"+elem);
//        String elem2 = queue.take();
//        System.out.println("elem"+elem2);

    }
}
